package com.ybwh.springboot2.common.mybatis.plugin.sass.ds;

import com.ybwh.springboot2.common.mybatis.plugin.sass.TenantIdHelper;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

/**
 * <p>
 * 基于独立数据库模式的sass方案
 * </p>
 *
 * @author fanbeibei
 * @version 1.0.0
 * @date 2022/12/27 11:42
 */
public class DefaultSassDataSource implements SassDataSource {
    private PrintWriter logWriter = new PrintWriter(System.out);


    private final DataSource globalDataSource;


    /**
     * tenantId -> DataSource
     */
    private final ConcurrentHashMap<String, DataSource> dataSourceMap = new ConcurrentHashMap<>();


    public DefaultSassDataSource(DataSource globalDataSource, Map<String, DataSource> dataSourceMap) {
        this.globalDataSource = globalDataSource;
        this.dataSourceMap.putAll(dataSourceMap);
    }

    public synchronized void init() {

    }


    public synchronized void destroy() {

    }


    @Override
    public String currentTenantId() {
        return TenantIdHelper.getTenantId();
    }


    @Override
    public synchronized DataSource getDataSource(String tenantId, boolean returnGlobalIfNull) {
        DataSource dataSource = dataSourceMap.get(tenantId);
        if (null != dataSource) {
            return dataSource;
        }
        if (returnGlobalIfNull) {
            return getGlobalDataSource();
        } else {
            return null;
        }

    }

    @Override
    public void addDataSource(String tenantId, DataSource dataSource) {
        dataSourceMap.put(tenantId, dataSource);
    }

    @Override
    public void removeDataSource(String tenantId) {
        dataSourceMap.remove(tenantId);

    }

    @Override
    public void replaceDataSource(String tenantId, DataSource dataSource) {
        dataSourceMap.put(tenantId, dataSource);
    }

    @Override
    public DataSource getGlobalDataSource() {
        return globalDataSource;
    }


    // ------------------------------- 以下是Datasource接口-------------------------------------------


    @Override
    public synchronized Connection getConnection() throws SQLException {
        String tenantId = currentTenantId();
        DataSource dataSource = dataSourceMap.get(tenantId);

        if (null != dataSource) {
            return dataSource.getConnection();
        }
        return null;
    }

    @Override
    public synchronized Connection getConnection(String username, String password) throws SQLException {
        String tenantId = currentTenantId();
        DataSource dataSource = dataSourceMap.get(tenantId);

        if (null != dataSource) {
            return dataSource.getConnection(username, password);
        }
        return null;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return logWriter;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {
        this.logWriter = out;
    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
        throw new SQLFeatureNotSupportedException("unsupported getLoginTimeout()");
    }

    @Override
    public int getLoginTimeout() throws SQLException {
        throw new SQLFeatureNotSupportedException("unsupported getLoginTimeout()");
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        throw new UnsupportedOperationException("unsupported unwrap(Class<T> iface)");
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        throw new UnsupportedOperationException("unsupported unwrap(Class<T> iface)");
    }


}
